import java.util.*;

public class leetCode003 {
    /**
     *  因为每次都要查找是否重复，O(n * list.size())
     */
    public static int lengthOfLongestSubstring1(String s) {
        int ans = 0;
        int len = s.length();
        List list = new ArrayList(len);
        /***
         *   使用一个list,每次判断是否重复，重复删除到重复数字地方，
         */
        for(int i = 0 ; i < len; i++){
            char ch = s.charAt(i);
            int index = list.indexOf(ch);

            if(index!=-1){
                list = list.subList(index+1,list.size());
            }
            list.add(ch);

            ans = Math.max(list.size(),ans);
        }
        return ans;
    }


    public static int lengthOfLongestSubstring2(String s) {
        char[] ch = s.toCharArray();
        int maxCount = 0;
        for(int i =0 ; i < ch.length; i++ ){
            Map<Character,Integer> chace = new HashMap<Character,Integer>();
            chace.put(ch[i],i);
            int j = i + 1;
            for(; j < ch.length; j++){
                if(chace.containsKey(ch[j])){
                    break;
                }else {
                    chace.put(ch[j],j);
                }
            }
            if(chace.size() > maxCount){
                maxCount = chace.size();
            }
        }
        return maxCount;
    }

    public static int lengthOfLongestSubstring3(String s) {
        int ans = 0;
        int len = s.length();
        Set<Character> querySet = new HashSet<Character>();
        for(int i = 0 ,r = 0 ; i < len; i++){
            char ch = s.charAt(i);
            while(querySet.contains(ch)){
                querySet.remove(s.charAt(r++));
            }
            querySet.add(ch);
            ans = Math.max(querySet.size(),ans);
        }
        return ans;
    }
    //官网答案
    public static int lengthOfLongestSubstring4(String s) {
        // 哈希集合，记录每个字符是否出现过
        Set<Character> occ = new HashSet<Character>();
        int n = s.length();
        // 右指针，初始值为 -1，相当于我们在字符串的左边界的左侧，还没有开始移动
        int rk = -1, ans = 0;
        for (int i = 0; i < n; ++i) {
            if (i != 0) {
                // 左指针向右移动一格，移除一个字符
                occ.remove(s.charAt(i - 1));
            }
            while (rk + 1 < n && !occ.contains(s.charAt(rk + 1))) {
                // 不断地移动右指针
                occ.add(s.charAt(rk + 1));
                ++rk;
            }
            // 第 i 到 rk 个字符是一个极长的无重复字符子串
            ans = Math.max(ans, rk - i + 1);
        }
        return ans;
    }

    public static int lengthOfLongestSubstring5(String s) {
        // 记录字符上一次出现的位置
        int[] last = new int[128];
        for(int i = 0; i < 128; i++) {
            last[i] = -1;
        }
        int n = s.length();

        int res = 0;
        int start = 0; // 窗口开始位置
        for(int i = 0; i < n; i++) {
            int index = s.charAt(i);
            start = Math.max(start, last[index] + 1);
            res   = Math.max(res, i - start + 1);
            last[index] = i;
        }

      /*  int[] last = new int[128];
//        for(int i = 0; i < 128; i++) {
//            last[i] = -1;
//        }
        int n = s.length();

        int res = 0;
        int start = 0; // 窗口开始位置
        for(int i = 0; i < n; i++) {
            int index = s.charAt(i);
            start = Math.max(start, last[index]);
            res   = Math.max(res, i - start + 1);
            last[index] = i+1;
        }

        return res;*/
        return res;
    }

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring5("pwwkew"));
    }
}
